package com.andreas.publishSubscribe;

import com.andreas.utils.ConnectionUtils;
import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 描述：消息接收者1
 */
public class Recer2 {
    public static void main(String[] args) throws IOException, TimeoutException {
        Connection connection = ConnectionUtils.getConnection();
        Channel channel = connection.createChannel();
        channel.queueDeclare("test_exchange_fanout_queue_2", false, false, false, null);
        /*
         * 参数1：队列名
         * 餐数2：交换器名称
         * 参数3：路由key（暂时无用，""即可）
         * */
        channel.queueBind("test_exchange_fanout_queue_2", "test_exchange_fanout", "");
        DefaultConsumer consumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                String msg = new String(body);
                System.out.println("【消费者2】 = " + msg);
            }
        };
        // 4.监听队列，true 自动消息确认
        channel.basicConsume("test_exchange_fanout_queue_2", true, consumer);
    }
}
